<?
include("session.php");
$date_start = date("Y-m-d")." 00:00:00";
$date_end = date("Y-m-d")." 23:59:59";
$yesterday_start = date("Y-m-d", strtotime("-1 day"))." 00:00:00";
$yesterday_end = date("Y-m-d", strtotime("-1 day"))." 23:59:59";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>电销助手外呼</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../../../webkit/utils/jquery.js"></script>
<script type="text/javascript" src="../../../webkit/grid/import.js"></script>
<script type="text/javascript" src="../../../webkit/dialog/import.js"></script>
<script type="text/javascript" src="../../../webkit/datepicker/WdatePicker.js"></script>
</head>
<body bgccolor="#f4f4f4" style="margin:0px;overflow-x:hidden">

<table width=100% height=100% cellspacing=0 cellpadding=0>
	<tr bgcolor="white">
	<form name=f>
	<td height=30>　
	任务名称：<select id="task_id" class='input' onchange="get_task_by_id(this.value)"><option value=''>-请选择-</option></select>　
	营销结果：<select id="call_result" class='input'><option value=''>-请选择-</option><option value='0' selected>未呼</option><option value='1'>营销成功</option><option value='2'>营销失败</option><option value='3'>预约回拔</option><option value='4'>集团彩铃</option><option value='5'>忙音</option><option value='6'>空号</option><option value='7'>停机</option><option value='8'>无人接听</option><option value='9'>直接挂机</option><option value='10'>非负责人</option><option value='11'>沟通障碍</option><option value='12'>无效数据</option></select>　
	时间范围：<input type="text" value="" id="date_start" name="date_start" onClick="WdatePicker({readOnly:true, skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" size="15"> - <input type="text" value="" id="date_end" name="date_end" onClick="WdatePicker({readOnly:true, skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" size="15"> <a href="javascript:set_date_range('<?echo $date_start?>', '<?echo $date_end?>')">今天</a>&nbsp;|&nbsp;<a href="javascript:set_date_range('<?echo $yesterday_start?>', '<?echo $yesterday_end?>')">昨天</a>　
	工号：<input type=text id='agent_id' size=8 disabled>
	<br>　
    电话号码：<input type=text id='telephone' size=12>　
	<input type="button" value="查询" onclick="search()"> <input type="button" value="下载录音" onclick="download_voice()" id="btn_download_voice"> <input type="button" value="导出记录" onclick="export_data()"  id="btn_export_data"> <input type="button" value="查录音" style="color:red" onclick="search_voice()"  id="btn_search_voice">
	　
	【<b style='color:#cc0000;'>输入号码外呼</b>】:<input type="text" id="m_tel" style="font-size:15px; font-weight:bold; height:16px; color:#3333ff; width:120px; border:gray 1px solid" onkeyup="if(event.keyCode==13){prepare()} else {$('#iframe_td').html('')}"><input type="button" value="确认号码" onclick="prepare()" style='border:gray 1px solid; height:20px; font-size:12px; font-weight:bold;color:#ff9900' id="btn_prepare_call"> <span id="iframe_td"></span>
	</td>
	</tr>
	<tr>
	<td>
		<table id="flex1" style="display:none"></table>
	</td>
	</form>
	</tr>
</table>
<iframe name="free" width=1 height=0></iframe>
<form name="dl_voice_form" method=post target="free"><input type="hidden" name="recordIds"></form>

<script>
	var agent_id = "<?echo $_GET['agent_id']?>";
	var h = window.screen.height-410; // 属于嵌入外部url，只能按screen高度大致计算
	//alert(h)
	if(h < 0) h = 'auto'
	$("#flex1").flexigrid({
		url: 'call_query.php?action=list&call_result=0&agent_id=<?echo $_SESSION['hngj_agent_id']?>', // 默认只显示未呼的记录
		dataType: 'json',
		colModel : [
			{display: 'ID', name : 'a', width : 30, sortable : false, align: 'center'},
			{display: '任务名称', name : 'a', width : 120, sortable : false, align: 'center'},
			{display: '客户姓名', name : 'a', width : 120, sortable : false, align: 'center'},
			{display: '彩铃', name : 'a', width : 40, sortable : false, align: 'center'},
			{display: '联系电话', name : 'b', width : 120, sortable : false, align: 'center'},
			{display: '座席工号', name : 'f', width : 60, sortable : true, align: 'center'},
			{display: '记录时间', name : 'f', width : 120, sortable : true, align: 'center'},
			{display: '营销结果', name : 'c', width : 60, sortable : true, align: 'center'},
			{display: '沟通内容', name : 'e', width : 150, sortable : true, align: 'center'},
			{display: '联系人', name : 'c', width : 60, sortable : true, align: 'center'},
			{display: '客户地址', name : 'c', width : 60, sortable : true, align: 'center'},
			{display: '服务号码', name : 'c', width : 80, sortable : true, align: 'center'},
			{display: '预约时间', name : 'b', width : 120, sortable : false, align: 'center'}
			],
		async : true,
		usepager: true,
		title: '外呼列表',
		useRp: true,
		rp: 12,
		showTableToggleBtn: false,
		width: 'auto',
		height: h,
		resizable : false,
		singleSelect : true
	});

	<?
	$add_url = "call_panel.php?op=add";
	$edit_url = "call_panel.php?op=edit&id="; 
	$del_url = "call_query.php?action=del&id=";
	$dlg_size = "780, 400";
	require("../../grid_common_js.php");
	?>

    $('#flex1').dblclick( function (e) { 
        select_row_on_dblclick(e);
        show_cust_info();
    });


	function set_date_range(a, b) {
		$('#date_start').val(a);
		$('#date_end').val(b);
	}

	function prepare() {
		var tel = $("#m_tel").val();
		if(tel == "") {
			alert("请输入客户电话号码！");
			return; 
		}
		if(callout_number == "") {
			alert("请选择一个任务，以获取外显号码配置！");
			return; 
		}
		$("#btn_prepare_call").attr("disabled", true);
		$.get("call_query.php?action=check_has_called&tel="+tel+"&task_id="+$("#task_id").val(), function(data) {
			if(data == "yes") {
				alert('客户号码('+tel+')已经拔打过，请避免重复操作！');
			}
			$("#btn_prepare_call").attr("disabled", false);
		});
		var str = "<iframe src='http://222.240.139.227:9080/agent_event/callout_button.jsp?caller="+callout_number+"&called="+$("#m_tel").val()+"&iconSize=20' width=21 height=21 frameborder=0 marginheight=0 marginwidth=0 scrolling=no align=top></iframe>";
		$("#iframe_td").html(str);
	}

	var is_recall = "no"; // 用于标识处理二次营销的callid记录;
	var recall_id = "";   // 二次营销callid

	function show_call_panel(telephone, call_tel, call_id, call_type) {
		dlg = new Dialog({ title: '外呼客户信息', resizable:false, maximizable:false}).href("call_panel.php?op=edit&telephone="+telephone+"&call_tel="+call_tel+"&call_type="+
			call_type+"&agent_id="+agent_id+"&call_id="+call_id+"&agent_id="+agent_id);
		dlg.show().setSize(780, 400).center();	
		$("#iframe_td").html(''); // 每次接通事件响应后，则清除掉外呼按钮，强制重新生成！
	}

    // 手动打开的外呼客户信息面板
	function show_cust_info(id) {
	   if(isNaN(id)) {
			id = $("#flex1").getSelectedRowId();
	   }
	   if(id != -1) {
			dlg = new Dialog({ title: '外呼客户信息', resizable:false, maximizable:false, closable:true, minimizable:false}).href("call_panel.php?op=edit&id="+id+"&agent_id="+agent_id);
			dlg.show().setSize(780, 350).center();
	   } else {
			alert("请选择一条记录进行操作！")
	   }
	}


	function search_voice() {
		dlg = new Dialog({ title: '查询录音', resizable:false, maximizable:false}).href("http://222.240.139.227:9080/agent_event/api/search_voice.jsp");
		dlg.show().setSize(600, 200).center();	
	}

	function search() { 
  		var url = "call_query.php?action=list&agent_id="+ agent_id + get_search_params();
		$('#flex1').flexOptions({'url': url}).flexReload(); 
	}

	function get_search_params() {
		var params = "";
		if($("#task_id").val()!="") {
            params += "&task_id="+ $("#task_id").val();
        }    
        if($("#call_result").val()!="") {
            params += "&call_result="+ $("#call_result").val();
        }    
	    if($("#telephone").val()!="") {
            params += "&telephone="+ $("#telephone").val();
        } 
	    if($("#agent_id").val()!="") {
            params += "&agent_id="+ $("#agent_id").val();
        } 
	    if($("#date_start").val()!="" && $("#date_end").val()!="") {
            params += "&date_range="+ $("#date_start").val()+"_"+$("#date_end").val();
        } 
		return params;
	}

	function show_voice(rid, tel) {
		dlg = new Dialog({ title: '查看录音', resizable:false, maximizable:false}).href("http://222.240.139.227:9080/agent_event/api/play_voice.jsp?rid="+rid+"&tel="+tel);
		dlg.show().setSize(540, 160).center();
	}

	function show_agent_score(task_id) {
		dlg = new Dialog({ title: '今日营销排名', resizable:false, maximizable:false}).href("agent_score_list.php?task_id="+task_id);
		dlg.show().setSize(650, 300).center();
	}


	function download_voice() {
		var call_result = $("#call_result").val();
		if(call_result==1 || call_result==2) {
			if(confirm('确定要下载当前记录的录音吗？')) {
				$('#btn_download_voice').val('处理中..');
				$('#btn_download_voice').attr('disabled', true);
				/*document.frames['free'].onReadyStateChange = function() {
					if (this.readyState && this.readyState == 'complete') {
						$('#btn_download_voice').val('下载录音');
						$('#btn_download_voice').attr('disabled', false);
					}
				}*/
				$.get("call_query.php?action=list&return_record_ids=yes" + get_search_params(), function(data){
					//alert(data.split(",").length)
					var dvf = document.dl_voice_form;
					dvf.action = "http://222.240.139.227:9080/agent_event/api/download_voice_zip.jsp";
					dvf.recordIds.value = data;
					//dvf.target="_blank";
					dvf.submit();
					$('#btn_download_voice').val('下载录音');
					$('#btn_download_voice').attr('disabled', false);
				});
			}
		} else {
			alert('只有营销成功或失败的记录，可以下载录音！');
		}
	}


	function export_data() {
		if(confirm('确定要导出当前记录到Excel表吗？')) {
			/*$('#btn_export_data').val('处理中..');
			$('#btn_export_data').attr('disabled', true);
			document.frames['free'].onReadyStateChange = function() {
				if (this.readyState && this.readyState == 'complete') {
					$('#btn_export_data').val('导出记录');
					$('#btn_export_data').attr('disabled', false);
				}
			}*/
			document.frames['free'].location = "call_query.php?action=list&export_to_excel=yes"+ get_search_params();
		}
	}


	$(function(){
		$.get("task_query.php?action=get_select_options", function(data) {
			var arr = data.split("$");
			for(var i=0; i<arr.length; i++){
				var kv = arr[i].split(',');
				f.task_id.options[i+1] = new Option(kv[1], kv[0]);
			}
		});

		if(agent_id == 'hncsgj00' || agent_id == 'hncsgj01'){
			$("#agent_id").attr('disabled', false);
		}
	});

	var callout_number = "";
	function get_task_by_id(id) {
		$.get("task_query.php?action=get&id="+id, function(data) {
			eval("var row = "+data);
			callout_number = row['callout_number'];
		});
	}

	//show_call_panel('15886379023', '11', 2);

	function set_recall_id(callid) {
		recall_id = callid;  // 保存一个全局变量，备用
		var frame = get_call_panel_frame();
		frame.document.getElementById('recall_id').value = callid;
	}

	function init_call_panel(telephone, call_tel, callid, call_type) {
		var frame = get_call_panel_frame();
		frame.document.getElementById('call_tel').value = call_tel;
		frame.document.getElementById('call_id').value = callid;
	}


	function get_call_panel_frame() {
		try{
			var fs = document.frames;
			for(var i=0; i<fs.length; i++) {
				if(fs[i].location.toString().indexOf("call_panel.php") >=0) {
					// 找到当前弹屏dialog的iframe
					return fs[i];
				}
			}
		} catch(e) {}
	}


</script>
</body>
</html>